<template>
  <div class="app-container">
    <el-row :gutter="20" class="div-height-left">
      <el-col :span="24" :xs="24" class="div-height-left">
        <el-row>
          <el-col :span="7">
            <!--                        药品列表组件-->
            <drug-info-table @rowClick="drugInfoRowClick" @selectAllReght="selectAllReght" />
          </el-col>
          <el-col :span="17">
<!--            <div class="search-container">-->
<!--              <el-form v-show="showSearch" ref="queryParamsRef" :inline="true" :model="queryParams" label-width="80px">-->
<!--                <el-form-item label="药品信息" prop="drugInfo">-->
<!--                  <el-input v-model="queryParams.drugName" clearable placeholder="请输入药品名称、代码、拼音码"-->
<!--                    @keyup.enter.native="handleQuery" />-->
<!--                </el-form-item>-->
<!--                <div style="float: right" class="mr20">-->
<!--                  <query-form-button @handleQuery="handleQuery" @resetQuery="resetQuery" />-->
<!--                </div>-->
<!--              </el-form>-->
<!--            </div>-->
            <el-row :gutter="10" class="mb8">
              <el-col :span="1.5">
                <el-button v-hasPerm="['base:price:export']" plain type="warning" @click="handleExport"><template #icon>
                    <i-ep-download /> </template>导出
                </el-button>
              </el-col>
            </el-row>
            <el-table v-adaptive :data="priceList" :border="true" highlight-current-row :stripe="true"
              @selection-change="handleSelectionChange" :header-cell-style="$headerCellStyle">
              <el-table-column :align="'center'" type="selection" width="55" />
              <el-table-column :show-overflow-tooltip="true" :align="'center'" fixed="left" label="是否供应"
                prop="supplySign" width="70px">
                <template #default="scope">
                  <dict-data-tag :options="dictDataMap['sys_yes_no']" :value="scope.row.supplySign" />
                </template>
              </el-table-column>
              <el-table-column :show-overflow-tooltip="true" :align="'left'" fixed="left" label="药品名称" prop="drugName"
                width="180px">
                <template #default="scope">
                  <span class="fz14 fw-bold">{{ scope.row.drugName }}</span>
                </template>
              </el-table-column>
              <el-table-column :show-overflow-tooltip="true" :align="'left'" label="药品编码" prop="drugCode"
                width="130px" />
              <el-table-column :show-overflow-tooltip="true" :align="'left'" label="规格" prop="packageSpec"
                width="120px" />
              <el-table-column :show-overflow-tooltip="true" :align="'center'" label="药品单位" prop="packageUnits" width="70px">
                <template #default="scope">
                  <dict-data-tag :options="dictDataMap['spec_unit']" :value="scope.row.packageUnits" />
                </template>
              </el-table-column>
              <el-table-column :show-overflow-tooltip="true" :align="'center'" label="进价(元)" prop="tradePrice"
                width="90px" />
              <el-table-column :show-overflow-tooltip="true" :align="'center'" label="售价(元)" prop="retailPrice"
                width="90px" />
              <el-table-column :show-overflow-tooltip="true" :align="'center'" label="起用日期" prop="startDate"
                width="140">
                <template #default="scope">
                  <span>{{
                    $parseTime(scope.row.startDate, "{y}-{m}-{d} {h}:{i}:{s}")
                  }}</span>
                </template>
              </el-table-column>
              <el-table-column :show-overflow-tooltip="true" :align="'center'" label="停止日期" prop="stopDate" width="140">
                <template #default="scope">
                  <span>{{
                    $parseTime(scope.row.stopDate, "{y}-{m}-{d} {h}:{i}:{s}")
                  }}</span>
                </template>
              </el-table-column>
              <el-table-column :show-overflow-tooltip="true" :align="'center'" label="最高限价(元)" prop="priceMax"
                width="100px" />
              <el-table-column :show-overflow-tooltip="true" :align="'center'" label="备注信息" prop="remarks" />
              <el-table-column :align="'center'" class-name="small-padding fixed-width" fixed="right" label="操作"
                width="260px">
                <template #default="scope">
                  <!--                                    <el-button-->
                  <!--                                        v-if="!scope.row.stopDate"-->
                  <!--                                        v-hasPerm="['base:priceList:priceAdjustment']"-->
                  <!--                                        icon="el-icon-edit"-->
                  <!--                                       -->
                  <!--                                        type="warning"-->
                  <!--                                        @click="priceAdjustment(scope.row)"-->
                  <!--                                    >调价-->
                  <!--                                    </el-button>-->
                  <el-button v-hasPerm="['base:price:remove']" :type="!scope.row.stopDate ? 'danger' : 'success'"
                    @click="stopPriceFun(scope.row)">
                    <svg-icon :icon-class="!scope.row.stopDate ? 'stop' : 'start'" class="mr5" />
                    {{ !scope.row.stopDate ? "停价" : "启用" }}
                  </el-button>

                  <el-button :type="scope.row.supplySign === 'Y' ? 'danger' : 'success'"
                    @click="stopSupplySign(scope.row)">
                    <svg-icon :icon-class="scope.row.supplySign === 'Y' ? 'stop' : 'start'" class="mr5" />
                    {{ scope.row.supplySign === "Y" ? "停止供应" : "恢复供应" }}
                  </el-button>
                  <el-button v-hasPerm="['base:price:edit']" type="success" @click="handleUpdate(scope.row)"><template
                      #icon>
                      <i-ep-edit /> </template>修改
                  </el-button>
                </template>
              </el-table-column>
            </el-table>
            <pagination v-show="total > 0" v-model:limit="queryParams.pageSize" v-model:page="queryParams.pageNum"
              :total="total" @pagination="getList" />
          </el-col>
        </el-row>
      </el-col>
    </el-row>
    <!-- 添加或修改药品价表对话框 -->
    <el-dialog :close-on-click-modal="false" :title="title" v-model="open" append-to-body width="1200px">
      <el-form ref="formRef" :model="form" :rules="rules" label-width="120px">
        <el-row :gutter="40">
          <el-col :span="8">
            <el-form-item label="药品名称" prop="drugCode">
              <el-input v-model="form.drugName" disabled placeholder="请输入药品编码" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="药品编码" prop="drugCode">
              <el-input v-model="form.drugCode" disabled placeholder="请输入药品编码" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="医保类型" prop="medicalInsuranceType">
              <el-select v-model="form.medicalInsuranceType" disabled placeholder="请选择医保类型" style="width: 100%">
                <el-option v-for="dict in dictDataMap['medical_insurance_type']" :key="dict.dictValue"
                  :label="dict.dictLabel" :value="dict.dictValue"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="规格" prop="packageSpec">
              <el-input v-model="form.packageSpec" disabled placeholder="请输入规格" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="药品单位" prop="packageUnits">
              <el-select v-model="form.packageUnits" disabled placeholder="请选择药品单位" style="width: 100%">
                <el-option v-for="dict in dictDataMap['spec_unit']" :key="dict.dictValue" :label="dict.dictLabel"
                  :value="dict.dictValue"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="进价" prop="tradePrice">
              <el-input v-model="form.tradePrice" disabled placeholder="请输入进价" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="市场零售价" prop="retailPrice">
              <el-input v-model="form.retailPrice" disabled placeholder="请输入市场零售价" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="起用日期" prop="startDate">
              <el-date-picker v-model="form.startDate" clearable disabled style="width: 100%" placeholder="请选择起用日期"
                type="datetime" value-format="yyyy-MM-dd HH:mm:ss">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="门诊费用分类" prop="classOnRcpt">
              <el-select v-model="form.classOnRcpt" placeholder="请选择门诊费用分类" style="width: 100%">
                <el-option v-for="dict in dictDataMap['outp_rcpt_fee_dict']" :key="dict.dictValue"
                  :label="dict.dictLabel" :value="dict.dictValue"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="住院费用分类" prop="classOnInp">
              <el-select v-model="form.classOnInp" placeholder="请选择住院费用分类" style="width: 100%">
                <el-option v-for="dict in dictDataMap['inp_rcpt_fee_dict']" :key="dict.dictValue"
                  :label="dict.dictLabel" :value="dict.dictValue"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="核算项目分类" prop="classOnReckoning">
              <el-select v-model="form.classOnReckoning" placeholder="请选择核算项目分类" style="width: 100%">
                <el-option v-for="dict in dictDataMap['reck_item_class_dict']" :key="dict.dictValue"
                  :label="dict.dictLabel" :value="dict.dictValue"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="会计科目" prop="subjCode">
              <el-select v-model="form.subjCode" placeholder="请选择会计科目" style="width: 100%">
                <el-option v-for="dict in dictDataMap['tally_subject_dict']" :key="dict.dictValue"
                  :label="dict.dictLabel" :value="dict.dictValue"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="病案首页分类" prop="classOnMr">
              <el-select v-model="form.classOnMr" placeholder="请选择病案首页分类" style="width: 100%">
                <el-option v-for="dict in dictDataMap['mr_fee_class_dict']" :key="dict.dictValue"
                  :label="dict.dictLabel" :value="dict.dictValue"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="最高限价" prop="priceMax">
              <el-input v-model="form.priceMax" disabled placeholder="请输入最高限价" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="网络价格" prop="networkPrice">
              <el-input v-model="form.networkPrice" disabled placeholder="请输入网络价格" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="备注信息" prop="remarks">
              <el-input v-model="form.remarks" placeholder="请输入备注信息" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="比例" prop="ratio">
              <el-input v-model="form.ratio" placeholder="请输入比例" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import {
  addPrice,
  delPrice,
  getPrice,
  selectPagePrice,
  stopPrice,
  supplySignStopOrRestore,
  updatePrice,
} from "@/api/drug/drugPrice";

import {useDictStoreHook} from "@/store/modules/dict";

const { proxy } = getCurrentInstance() as any;
const dictStore = useDictStoreHook();
const dictDataMap = ref(
  dictStore.isTypeGetData(
    "tally_subject_dict",
    "outp_rcpt_fee_dict",
    "inp_rcpt_fee_dict",
    "spec_unit",
    "reck_item_class_dict",
    "mr_fee_class_dict",
    "sys_yes_no",
    "medical_insurance_type"
  )
);
const formRef = ref();
const buttonLoading = ref(false);
// 选中数组
const ids = ref([]);
// 非单个禁用
const single = ref(true);
// 非多个禁用
const multiple = ref(true);
// 显示搜索条件
const showSearch = ref(true);
// 总条数
const total = ref(0);
// 药品字典基本信息表格数据
const drugInfoList = ref([]);
// 弹出层标题
const title = ref("");
// 药品价表表格数据
const priceList = ref([]);
// 是否显示弹出层
const open = ref(false);
// 查询参数
const queryParamsRef = ref();
const queryParams = ref<any>({
  pageNum: 1,
  pageSize: 10,
  drugCode: undefined,
  drugInfoId: undefined,
  medicalInsuranceType: undefined,
  priceId: undefined,
  startDate: undefined,
  stopDate: undefined,
  supplySign: undefined,
  packSize: undefined,
  orgId: undefined,
  remarks: undefined,
  ratio: undefined,
});
// 表单参数
const form = ref<any>({});
// 表单校验
const rules = ref({
  packageSpec: [{ required: true, message: "规格不能为空", trigger: "blur" }],
  packageUnits: [{ required: true, message: "药品单位不能为空", trigger: "change" }],
  tradePrice: [{ required: true, message: "进价不能为空", trigger: "blur" }],
  retailPrice: [{ required: true, message: "售价不能为空", trigger: "blur" }],
  startDate: [{ required: true, message: "起用日期不能为空", trigger: "blur" }],
  classOnRcpt: [{ required: true, message: "门诊费用分类不能为空", trigger: "change" }],
  classOnInp: [{ required: true, message: "住院费用分类不能为空", trigger: "change" }],
  classOnReckoning: [
    { required: true, message: "核算项目分类不能为空", trigger: "change" },
  ],
  subjCode: [{ required: true, message: "会计科目不能为空", trigger: "change" }],
  classOnMr: [{ required: true, message: "病案首页分类不能为空", trigger: "change" }],
});

/** 停价 */
function stopPriceFun(row) {
  ElMessageBox.confirm(
    `确定要${!row.stopDate ? "停用" : "启用"}"${row.drugName}"此药品吗？`,
    {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    }
  )
    .then(() => {
      row.stopDate = !row.stopDate ? new Date().getTime() : "";
      stopPrice(row).then(() => {
        getList();
        ElMessage.success(`${row.supplySign === "Y" ? "已停价" : "已启价"}`);
      });
    })
    .catch(() => { });
}
/** 停止供应 */
function stopSupplySign(row) {
  ElMessageBox.confirm(
    `确定要将"${row.drugName}"药品${row.supplySign === "Y" ? "停止供应" : "恢复供应"
    }吗？`,
    {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    }
  )
    .then(() => {
      row.supplySign = row.supplySign === "Y" ? "N" : "Y";
      supplySignStopOrRestore(row).then(() => {
        getList();
        ElMessage.success(`${row.supplySign === "Y" ? "已恢复供应" : "已停止供应"}`);
      });
    })
    .catch(() => { });
}
function selectAllReght(orgId) {
  queryParams.value.orgId = orgId;
  getList();
}
/** 左侧药品字典点击事件 */
function drugInfoRowClick(row) {
  queryParams.value.drugInfoId = row.id;
  queryParams.value.orgId = row.orgId;
  getList();
}
/** 查询药品价表列表 */
function getList() {
  if (!queryParams.value.orgId) {
    return ElMessage.error(proxy.$OrgIdIsEmpty);
  }
  selectPagePrice(queryParams.value).then((response: any) => {
    priceList.value = response.rows;
    total.value = response.total;
  });
}
// 取消按钮
function cancel() {
  open.value = false;
  reset();
}
// 表单重置
function reset() {
  form.value = {
    id: undefined,
    drugCode: undefined,
    drugInfoId: undefined,
    medicalInsuranceType: undefined,
    priceId: undefined,
    packageSpec: undefined,
    packageUnits: undefined,
    networkPrice: undefined,
    tradePrice: undefined,
    retailPrice: undefined,
    startDate: undefined,
    stopDate: undefined,
    classOnRcpt: undefined,
    classOnInp: undefined,
    classOnReckoning: undefined,
    subjCode: undefined,
    classOnMr: undefined,
    priceMax: undefined,
    supplySign: undefined,
    packSize: undefined,
    orgId: undefined,
    delFlag: undefined,
    createBy: undefined,
    createTime: undefined,
    updateBy: undefined,
    updateTime: undefined,
    remarks: undefined,
    ratio: undefined,
  };
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  queryParamsRef.value.resetFields();
  handleQuery();
}
// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length !== 1;
  multiple.value = !selection.length;
}
/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加药品价表";
}
/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const id = row.id || ids.value;
  getPrice(id).then((response) => {
    form.value = response.data;
    form.value.drugName = row.drugName;
    open.value = true;
    title.value = "修改药品价表";
  });
}
/** 提交按钮 */
function submitForm() {
  (formRef.value as any).validate((valid) => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.id != null) {
        updatePrice(form.value)
          .then((_response) => {
            ElMessage.success("修改成功");
            open.value = false;
            getList();
          })
          .finally(() => {
            buttonLoading.value = false;
          });
      } else {
        addPrice(form.value)
          .then((_response) => {
            ElMessage.success("新增成功");
            open.value = false;
            getList();
          })
          .finally(() => {
            buttonLoading.value = false;
          });
      }
    }
  });
}
/** 删除按钮操作 */
function handleDelete(row) {
  const idsThis = row.id || ids.value;
  ElMessageBox.confirm('是否确认删除药品价表编号为"' + idsThis + '"的数据项？', {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      delPrice(ids).then((_res) => {
        getList();
        ElMessage.success("删除成功");
      });
    })
    .catch(() => { });
}
/** 导出按钮操作 */
function handleExport() {
  // download('base/price/export', {
  //     ...queryParams
  // }, `price_${new Date().getTime()}.xlsx`)
}
</script>

<script lang="ts">
export default {
  name: "drugPrice",
};
</script>

<style scoped></style>
